<?php
/**
 * 展示所有合伙人相关报表
 */

class Action
{
    private $per_page = 50;

    /**
     *
     */
    public function index(){
        $dealer_level      = Request::Get('dealer_level', 0);
        $page          = Request::Get('page_no', 1);
        $is_excel      = Request::getParam('is_excel', 0);
        $search_filter      = Request::getParam('search_filter', array());

        // 合伙人过滤
        if (!$dealer_level) {
            $search_filter['dealer_level > '] = 0;
        } else {
            $search_filter['dealer_level'] = $dealer_level;
        }

        if(isset($search_filter['mobile']) && $search_filter['mobile']){
            $search_filter['`mobile` LIKE '] = '%' . $search_filter['mobile'] . '%';
        } else {
            unset($search_filter['mobile']);
        }

        $search_filter['status'] = 1;
        $search_filter['is_enable'] = 1;

        $area_list = _model('region')->getList(array('area_code >' => 0));
        $area_list_new = array();
        foreach($area_list as $item){
            $area_list_new[$item['area_code']] = $item['area'];
        }

        $order_count = _model('user')->getTotal(enable_filter($search_filter));
        $pager = new Pager($this->per_page);
        if ($pager->generate($order_count)) {
            Response::assign('pager', $pager);
        }
        $order_by = ' ORDER BY `id` DESC ';
        if (!$is_excel) {
            $order_by .= $pager->getLimit($page);
        }
        $user_list = _model('user')->getList(enable_filter($search_filter), $order_by);

        foreach ($user_list as $k=>$item) {
            $price = _model('sku')->read(array('id'=>85)); //只比较散装爆米花的价格
            $user_list[$k]['price'] = $price['price'];

            if($price['diff_price'] && $item['dealer_level'] == 2){ //二级合伙人找差价
                $user_list[$k]['price'] = $user_list[$k]['price'] + $price['diff_price'];
            }

            $user_list[$k]['parent_realname'] = '';
            if($item['parent_id']){
                $parent_info = _model('user')->read(array('id' => $item['parent_id']));
                $user_list[$k]['parent_realname'] = $parent_info['realname'];
            }

            $user_area = _model('dealer_address')->read(array('user_id' => $item['id']));

            $user_list[$k]['province_name'] =  @$area_list_new[$user_area['province_code']];
            $user_list[$k]['city_name'] =  @$area_list_new[$user_area['city_code']];
            $user_list[$k]['county_name'] =  @$area_list_new[$user_area['county_code']];

            //获取终端数量
            $store_list = _model('store')->getList(array('agency_id' => $item['id']));
            $user_list[$k]['store_count'] = count($store_list);

            //获取绑定数量
            $device_count = array(
                'MX' => 0,
                'ZX' => 0,
                'FX' => 0,
                'AD' => 0,
                'TA' => 0,
                'OS' => 0,
                'CS' => 0,
                );

            foreach($store_list as $k1=>$item){
                $relation_info = _model('store_device_relation')->getList(array('store_id' => $item['id'], 'status' => 2));
                foreach($relation_info as $k2 => $v1){
                    if($v1['type'] == 'MX'){
                        $device_count['MX'] ++;
                    } elseif($v1['type'] == 'ZX'){
                        $device_count['ZX'] ++;
                    } elseif($v1['type'] == 'FX'){
                        $device_count['FX'] ++;
                    } elseif($v1['type'] == 'AD'){
                        $device_count['AD'] ++;
                    } elseif($v1['type'] == 'TA'){
                        $device_count['TA'] ++;
                    } elseif($v1['type'] == 'OS'){
                        $device_count['OS'] ++;
                    } elseif($v1['type'] == 'CS'){
                        $device_count['CS'] ++;
                    }
                }
            }
            $user_list[$k]['device_count'] = $device_count;
        }


        if($is_excel) {
            $this->export_excel($user_list);
        }

        Response::assign('user_list', $user_list);
        Response::assign('search_filter', $search_filter);
        Response::display('admin/dealer_brief.html');
    }

    public function export_excel($list = array()){
        $lines = array();
        foreach($list as $k=>$v){
            array_push($lines, array(
                $v['realname'],
                $v['user_name'],
                $v['id'],
                $v['province_name'],
                $v['city_name'],
                $v['county_name'],
                $v['dealer_level'],
                $v['price'],
                ($v['parent_id']?$v['parent_id']:''),
                $v['parent_realname'],
                $v['store_count'],
                $v['device_count']['ZX'],
                $v['device_count']['MX'],
                $v['device_count']['FX'],
                $v['device_count']['OS'],
                $v['device_count']['AD'],
                $v['device_count']['TA'],
            ));
        }
        list($excelObj, $excelWriter) = _widget('tools.excel')->getExcelObj();
        // 哪几列
        $col_arr = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O','P','Q');
        // 第一排的标题
        $col_title_arr = array('姓名','用户名','id','省份','市','县/区','合伙人级别','散装进货价格','对应一级合伙人id','对应一级合伙人姓名','终端数量','二代箱体绑定数量','三代箱体绑定数量','四代箱体绑定数量','摇一摇模块数量','液晶屏数量','游戏桌数量');

        foreach ($col_arr as $k => $v) {
            //设置单位格内容A1,B1,C1,D1
            $excelObj->setCellValue($v."1", $col_title_arr[$k]);
        }

        $key = 2;
        foreach ($list as $k=>$v) {
            foreach ($col_arr as $k2=>$v2) {
                $place = $v2.$key;
                $excelObj->setCellValue($place, $lines[$k][$k2]);
            }
            $key++;
        }
        $filename = '合伙人设备简报';
        return _widget('tools.excel')->download($excelWriter, $filename);
    }
}
